package models

import (
	"time"

	. "gddgame.cc/galaxy/common/dsl/db"
	"gddgame.cc/galaxy/utils/orm"
)

var (
	UserOpenModel = orm.NewModel("user_open", &UserOpen{}, nil)
)

func init() {
	AddTable(&Table{
		Model: UserOpenModel,
	})
}

// 用户按app获取到不同的open信息
// 用于区分不同app中用户的昵称和进行登录凭证的判断
type UserOpen struct {
	Id       string `json:"id" orm:"primary" sql:"VARCHAR"`
	UserId   int    `json:"user_id" sql:"INT"`
	AppId    int    `json:"app_id" sql:"INT"`
	NickName string `json:"nick_name" sql:"VARCHAR"`
	Avatar   string `json:"avatar" sql:"VARCHAR"`
	Token    string `json:"token" sql:"VARCHAR"`

	// 分享来源用户ID
	OriginOpenId string `json:"origin_open_id" sql:"VARCHAR"`

	WechatOpenId          string `json:"wechat_open_id" sql:"VARCHAR"`
	FacebookOpenId        string `json:"facebook_open_id" sql:"VARCHAR"`
	QQOpenId              string `json:"qq_open_id" sql:"VARCHAR"`
	AppleGameCenterOpenId string `json:"apple_game_center_open_id" sql:"VARCHAR"`
	GoogleGamePlayOpenId  string `json:"google_game_play_open_id" sql:"VARCHAR"`
	MiGameOpenId          string `json:"mi_game_open_id" sql:"VARCHAR"`
	HuaweiOpenId          string `json:"huawei_open_id" sql:"VARCHAR"`
	UcOpenId              string `json:"uc_open_id" sql:"VARCHAR"`

	Preferences map[string]string      `json:"preferences" sql:"JSON"`
	Store       map[string]interface{} `json:"store" sql:"JSON"`

	LastTime   *time.Time `json:"last_time" sql:"DATETIME"`
	CreateTime *time.Time `json:"create_time" orm:"createTime" sql:"DATETIME"`
	UpdateTime *time.Time `json:"update_time" orm:"updateTime" sql:"DATETIME"`
	DeleteTime *time.Time `json:"delete_time" orm:"deleteTime" sql:"DATETIME"`
}

func (user *UserOpen) GetInfo() map[string]interface{} {
	info := map[string]interface{}{
		"user_id":   user.Id,
		"nick_name": user.NickName,
		"avatar":    user.Avatar,
		"token":     user.Token,
		"last_time": "",
	}

	if user.LastTime != nil {
		info["last_time"] = user.LastTime.String()
	}
	return info
}
